﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrinterMonitorSvc.Ext
{
    /// <summary>
    /// 枚举扩展方法
    /// </summary>
    public static class EnumExt
    {
        #region 获取枚举的描述值
        /// <summary>
        /// 获取枚举的描述值
        /// </summary>
        /// <param name="en">源枚举</param>
        /// <returns>描述值</returns>
        public static string ValueOfDescription(this Enum en)
        {
            var type = en.GetType();
            var memInfo = type.GetMember(en.ToString());
            if (memInfo.Length <= 0) return en.ToString();
            var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attrs.Length > 0 ? ((DescriptionAttribute)attrs[0]).Description : en.ToString();
        }
        #endregion

        #region 获取枚举数字值的字符串格式
        /// <summary>
        /// 获取枚举数字值的字符串格式
        /// </summary>
        /// <param name="en">源枚举</param>
        /// <returns></returns>
        public static string ValueOfEnum(this Enum en)
        {
            return Convert.ToInt32(en).ToString();
        }
        #endregion
    }
}
